package com.sparrow.base.handler;

import com.sparrow.annotation.bean.RequestUriInfo;
import com.sparrow.base.handler.bean.BindExceptionResponseData;
import com.sparrow.config.LogConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2018/12/23.
 */
@RestController
@RestControllerAdvice
public class GlobalExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final  Integer BIND_ERROR_CODE = 502;
    private final  Integer EXCEPTION_ERROR_CODE = 501;


    /**
     * 对象参数bind 异常
     *
     * @param request
     * @param ex
     * @return
     */
    @ExceptionHandler(BindException.class)
    public BindExceptionResponseData exceptionHandler(HttpServletRequest request, BindException ex) {
        String url = request.getRequestURI();
        StringBuilder errorMesssage = new StringBuilder("校验失败:");
        errorMesssage.append(ex.getFieldErrors()
                .stream()
                .map(FieldError::getDefaultMessage)
                .collect(Collectors.joining(",")));
        return new BindExceptionResponseData(BIND_ERROR_CODE, url, errorMesssage.toString());
    }

    /**
     * 普通参数bind异常
     *
     * @param request
     * @param ex
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public BindExceptionResponseData exception2Handler(HttpServletRequest request, ConstraintViolationException ex) {
        String url = request.getRequestURI();
        StringBuilder errorMesssage = new StringBuilder("校验失败:");
        errorMesssage.append(ex.getMessage());
        return new BindExceptionResponseData(BIND_ERROR_CODE, url, errorMesssage.toString());
    }
    /**
     * 普通参数bind异常
     *
     * @param request
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    public BindExceptionResponseData GlobalControllerException(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        List<RequestUriInfo> requestMappingList = LogConfig.requestMappingList;
        String url = request.getRequestURI();
        RequestUriInfo current = null;
        for (RequestUriInfo uri:requestMappingList) {
            if (uri.getWholeUri().equals(url)){
                current = uri;
                break;
            }
        }
        String message = String.format("%s Fail:",current.getName());
        StringBuilder errorMesssage = new StringBuilder(message);
        errorMesssage.append(ex.getMessage());
        logger.error("",ex);
        return new BindExceptionResponseData(EXCEPTION_ERROR_CODE, url, errorMesssage.toString());
    }
}
